package com.pig4cloud.pig.village.villager.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.village.api.dto.VillagerDTO;
import com.pig4cloud.pig.village.api.dto.VillagerSelect;
import com.pig4cloud.pig.village.api.entity.Villager;
import com.pig4cloud.pig.village.api.request.VillagerSearch;
import com.pig4cloud.pig.village.villager.service.IVillagerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author limbray.li
 * @date 2021/6/26
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/villager")
@Api(value = "villager", tags = "村民")
public class VillagerController {
	@Resource
	private IVillagerService villagerService;

	@ApiOperation(value = "查询全部村民-all")
	@GetMapping("/list")
	public R<List<Villager>> list() {
		return R.ok(villagerService.list());
	}

	@ApiOperation(value = "村民基础信息查询")
	@GetMapping("/pageBySearch")
	public R<IPage<VillagerDTO>> listBySearch(Integer pageNum, Integer pageSize, VillagerSearch villagerSearch) {
		return R.ok(villagerService.pageBySearch(new Page(pageNum, pageSize), villagerSearch));
	}

	@ApiOperation(value = "全部村民下拉框-all")
	@GetMapping("/listVillagerAllSelect")
	public R<List<VillagerSelect>> listVillagerAllSelect() {
		return R.ok(villagerService.listAllSelect());
	}

	@ApiOperation(value = "全部村民下拉框-by 户id")
	@GetMapping("/listVillagerByFamilyIdSelect")
	public R<List<VillagerSelect>> listByFamilyIdSelect(Long familyId) {
		return R.ok(villagerService.listByFamilyIdSelect(familyId));
	}

	@ApiOperation(value = "全部村民下拉框-by 组id")
	@GetMapping("/listVillagerByTeamIdSelect")
	public R<List<VillagerSelect>> listByTeamIdSelect(Long teamId) {
		return R.ok(villagerService.listByTeamIdSelect(teamId));
	}

	@ApiOperation(value = "根据村民id单个删除")
	@PutMapping("/deleteById")
	public R deleteById(Long id){
		return R.ok(villagerService.removeById(id));
	}

	@ApiOperation(value = "根据村民id集合批量删除")
	@PutMapping("/deleteByIdList")
	public R deleteById(@RequestParam(value = "idList") @ApiParam(value = "id列表") List<Integer> idList){
		return R.ok(villagerService.removeByIds(idList));
	}

	@ApiOperation(value = "新增或更新村民信息")
	@PostMapping("/addOrUpdate")
	public R addOrUpdateVillager(@RequestBody VillagerDTO villagerInfo){
//		return R.ok(villagerService.saveOrUpdate(villagerInfo));
		return R.ok(villagerService.addOrUpdateVillager(villagerInfo));
	}
}
